/**
 * 
 */
package com.socialvampic.mvc.view;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.socialvampic.language.Language;
import com.socialvampic.language.OnChangedLanguageListener;

/**
 * @author trungduy
 * 
 */
public abstract class AbstractView extends LinearLayout implements Observer {
	protected OnChangedLanguageListener mLanguageListener;
	private SharedPreferences mLanguageSharedPreferences;

	public AbstractView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		setLayoutParams(layoutParams);
		mLanguageSharedPreferences = context.getApplicationContext()
				.getSharedPreferences(Language.LOCALE, Activity.MODE_PRIVATE);
	}

	public AbstractView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		setLayoutParams(layoutParams);
		mLanguageSharedPreferences = context.getApplicationContext()
				.getSharedPreferences(Language.LOCALE, Activity.MODE_PRIVATE);
	}

	@Override
	public void update(Object object) {
		// TODO Auto-generated method stub
		if (object instanceof Language) {
			if (mLanguageListener != null) {
				mLanguageListener.onLanguageChanged();
			}
		}
	}

	public void setLanguageListener(OnChangedLanguageListener listener) {
		mLanguageListener = listener;
	}

	/**
	 * Initialize UI
	 * 
	 * Register listener through Interface
	 * 
	 * @param inflater
	 * @param container
	 */
	public abstract void createView(LayoutInflater inflater, ViewGroup container);

	/**
	 * Release UI element
	 */
	public void destroyView() {
		mLanguageSharedPreferences = null;
		setLanguageListener(null);
	}

	/**
	 * Get string code of current language application
	 * 
	 * @return
	 */
	public String getCurrentLanguageCode() {
		return mLanguageSharedPreferences.getString(Language.LOCALE,
				Language.EN_KEY);
	}

	/**
	 * Refresh display content of View
	 * 
	 * For example : when change language
	 */
	public abstract void reloadView();
}
